﻿using CastingWellEquipmentGUI.Config;
using CastingWellEquipmentGUI.Dao;
using CastingWellEquipmentGUI.Model;
using CastingWellEquipmentGUI.ViewModel.Common.LogTool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CastingWellEquipmentGUI.Utils
{
    public static class TagTool
    {
        /// <summary>
        /// 根据窗体名称和控件名称获取标签值
        /// </summary>
        /// <param name="FormName"></param>
        /// <param name="ControlName"></param>
        /// <returns></returns>
        public static string GetTagValueByFormNameAndControlName(string FormName, string ControlName)
        {
            int tagId = GetTagIdByValueByFormNameAndControlName(FormName, ControlName);
            if (tagId == 0)
            {
                return "标签未配置";
            }
            TagInfo tagInfo = null;
            if (SystemConfig.SystemStatus != "网络中断")
            {
                tagInfo = RedisHelper.GetHashKey<TagInfo>("Tag_Value", tagId.ToString());
            }
            if (tagInfo != null)
            {
                if (tagInfo.DataType == "Real")
                {
                    return Math.Floor(Convert.ToDecimal(tagInfo.Value)).ToString();
                }
                return tagInfo.Value;
            }
            else {
                return "";
            }
        }
        /// <summary>
        /// 根据标签ID在Redis中获取值
        /// </summary>
        /// <param name="TagId"></param>
        /// <returns></returns>
        public static string GetTagValueById(int TagId)
        {
            TagInfo tagInfo = null;
            if (SystemConfig.SystemStatus != "网络中断")
            {
                tagInfo = RedisHelper.GetHashKey<TagInfo>("Tag_Value", TagId.ToString());
            }
            if (tagInfo != null)
            {
                return tagInfo.Value;
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 根据窗体名称和控件名称获取标签ID
        /// </summary>
        /// <param name="FormName">窗体名称</param>
        /// <param name="ControlName">控件名称</param>
        /// <returns></returns>
        public static int GetTagIdByValueByFormNameAndControlName(string FormName, string ControlName)
        {
            for (int i = 0; i < LogTool.CurrentUser.Roles.Count; i++)
            {
                for (int j = 0; j < LogTool.CurrentUser.Roles[i].Permissions.Count; j++)
                {
                    if (LogTool.CurrentUser.Roles[i].Permissions[j].PForm != null &&
                        LogTool.CurrentUser.Roles[i].Permissions[j].PForm.FormName == FormName &&
                        LogTool.CurrentUser.Roles[i].Permissions[j].PControl.ControlName == ControlName)
                    {
                        return LogTool.CurrentUser.Roles[i].Permissions[j].PControl.TagInfoId;
                    }

                }

            }
            return 0;
        }

        /// <summary>
        /// 根据窗体名称和控件名称寻找控件
        /// </summary>
        /// <param name="FormName"></param>
        /// <param name="ControlName"></param>
        /// <returns></returns>
        private static Control GetControlByFormNameAndControlName(string FormName, string ControlName)
        {
            Control control = null;
            bool IsBreak = false;
            for (int i = 0; i < LogTool.CurrentUser.Roles.Count; i++)
            {
                for (int j = 0; j < LogTool.CurrentUser.Roles[i].Permissions.Count; j++)
                {
                    if (LogTool.CurrentUser.Roles[i].Permissions[j].PForm != null &&
                        LogTool.CurrentUser.Roles[i].Permissions[j].PForm.FormName == FormName &&
                        LogTool.CurrentUser.Roles[i].Permissions[j].PControl.ControlName == ControlName)
                    {
                        control = LogTool.CurrentUser.Roles[i].Permissions[j].PControl;
                        IsBreak = true;
                        break;
                    }

                }
                if (IsBreak)
                {
                    break;
                }
            }
            return control;
        }

        /// <summary>
        /// 根据窗体名称和控件名称获取弹出窗体的标签数据列表
        /// </summary>
        /// <param name="FormName"></param>
        /// <param name="ControlName"></param>
        /// <param name="WindowName"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public static List<ControlAndWindowTag> GetTagsByFormNameAndControlName(string FormName, string ControlName,string WindowName)
        {
            Control control = GetControlByFormNameAndControlName(FormName, ControlName);
            string sql = @"SELECT *
                          FROM DB_CWEGUI.dbo.ControlAndWindowTag
                          WHERE ControlId = "+ control.ControlId + @"
                          AND WindowName = '"+ WindowName + "'";
            return DapperToolsThisSystem.Query<ControlAndWindowTag>(sql);
        }
    }
}
